﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using System.Text;

namespace Runda.CMS.Portal.MyDesktop
{
    public partial class MyDesktop : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }
        private SysDesktopWebPartsBll deskWebPartBll = new SysDesktopWebPartsBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<SysDesktopWebPartsEntity> entitys = new List<SysDesktopWebPartsEntity>();
                if (Request.QueryString["ispublic"] != null && Request.QueryString["ispublic"].ToString() != "")
                {
                    entitys = deskWebPartBll.GetAll<SysDesktopWebPartsEntity>(" and DesktopID=(select top 1 DesktopID from Cms_Sys_Desktop where IsPublic=1)");
                }
                else
                {
                    if (this.Session["CurrentUserID"] == null)
                    {
                        Response.Redirect("/Error.aspx?Error=" + System.Convert.ToInt32(Enumerator.ErrorType.SessionAbandon).ToString());
                    }
                    entitys = deskWebPartBll.GetAll<SysDesktopWebPartsEntity>("and DesktopID=(select top 1 DesktopID from Cms_Sys_Desktop  where Owner='" + this.Session["CurrentUserID"].ToString() + "' or Owner is null order by IsDefault desc,IsPublic asc)");
                }
                if (entitys.Count > 0)
                {
                    bool flag3 = false;
                    bool flag4 = false;
                    bool flag5 = false;
                    short num6 = 0;
                    short num7 = 0;
                    short num8 = 0;
                    StringBuilder builder = new StringBuilder();
                    List<SysDesktopWebPartsEntity> list5 = new List<SysDesktopWebPartsEntity>();
                    foreach (SysDesktopWebPartsEntity entity in entitys)
                    {
                        if (entity.WpType == "0000029901")
                        {
                            list5.Add(entity);
                        }
                    }

                    foreach (SysDesktopWebPartsEntity entity in list5)
                    {
                        builder.Append("<A href=\"" + entity.URL + "\"  target=\"" + entity.Target + "\"  class=\"hl\" " + (entity.Target == "_self" ? " onclick='addLocation(\"" + entity.WPName.ToString() + "\",\"" + entity.URL + "\",\"stage\")'" : "") + " >" + entity.WPName.ToString() + "</A>&nbsp;&nbsp;");
                    }
                    this.ltlRushChannel.Text = builder.ToString();
                    builder = new StringBuilder();

                    List<SysDesktopWebPartsEntity> list = new List<SysDesktopWebPartsEntity>();
                    foreach (SysDesktopWebPartsEntity entity in entitys)
                    {
                        if (entity.WpType == "0000029902" && entity.Area == 1)
                        {
                            list.Add(entity);
                        }
                    }

                    List<SysDesktopWebPartsEntity> list2 = new List<SysDesktopWebPartsEntity>();
                    foreach (SysDesktopWebPartsEntity entity in entitys)
                    {
                        if (entity.WpType == "0000029902" && entity.Area == 2)
                        {
                            list2.Add(entity);
                            flag3 = true;
                        }
                    }

                    List<SysDesktopWebPartsEntity> list3 = new List<SysDesktopWebPartsEntity>();
                    foreach (SysDesktopWebPartsEntity entity in entitys)
                    {
                        if (entity.WpType == "0000029902" && entity.Area == 3)
                        {
                            list3.Add(entity);
                            flag4 = true;
                        }
                    }

                    List<SysDesktopWebPartsEntity> list4 = new List<SysDesktopWebPartsEntity>();
                    foreach (SysDesktopWebPartsEntity entity in entitys)
                    {
                        if (entity.WpType == "0000029902" && entity.Area == 4)
                        {
                            list4.Add(entity);
                            flag5 = true;
                        }
                    }

                    List<SysDesktopWebPartsEntity> list6 = new List<SysDesktopWebPartsEntity>();
                    foreach (SysDesktopWebPartsEntity entity in entitys)
                    {
                        if (entity.WpType == "0000029902" && entity.Area == 5)
                        {
                            list5.Add(entity);                         
                        }
                    }
                    builder.Append(this.GetHTMLOfArea(list));
                   
                    if ((!flag3 & !flag4) & !flag5)
                    {
                        num6 = 0;
                        num7 = 0;
                        num8 = 0;
                    }
                    else if ((flag3 & !flag4) & !flag5)
                    {
                        num6 = 100;
                        num7 = 0;
                        num8 = 0;
                    }
                    else if ((!flag3 & flag4) & !flag5)
                    {
                        num6 = 0;
                        num7 = 100;
                        num8 = 0;
                    }
                    else if ((!flag3 & !flag4) & flag5)
                    {
                        num6 = 0;
                        num7 = 0;
                        num8 = 100;
                    }
                    else if ((flag3 & flag4) & flag5)
                    {
                        num6 = 0x19;
                        num7 = 50;
                        num8 = 0x19;
                    }
                    else if ((flag3 & flag4) & !flag5)
                    {
                        num6 = 0x19;
                        num7 = 0x4b;
                        num8 = 0;
                    }
                    else if ((!flag3 & flag4) & flag5)
                    {
                        num6 = 0;
                        num7 = 0x4b;
                        num8 = 0x19;
                    }
                    else if ((flag3 & !flag4) & flag5)
                    {
                        num6 = 50;
                        num7 = 0;
                        num8 = 50;
                    }
                    builder.Append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                    builder.Append("<tr>");
                    builder.Append("<td width=\"" + num6.ToString() + "%\" valign=\"top\" style=\"" + (flag3 ? "padding-left:0px;padding-right:6px;" : "") + "\">");
                    builder.Append(this.GetHTMLOfArea(list2));
                    builder.Append("</td>");
                    builder.Append("<td width=\"" + num7.ToString() + "%\" valign=\"top\" style=\"" + (flag4 ? "padding-left:6px;padding-right:6px;" : "") + "\">");
                    builder.Append(this.GetHTMLOfArea(list3));
                    builder.Append("</td>");
                    builder.Append("<td width=\"" + num8.ToString() + "%\" valign=\"top\" style=\"" + (flag5 ? "padding-left:6px;padding-right:0px;" : "") + "\">");
                    builder.Append(this.GetHTMLOfArea(list4));
                    builder.Append("</td>");
                    builder.Append("</tr>");
                    builder.Append("</table>");
                    builder.Append(this.GetHTMLOfArea(list6));

                    //builder.Append(this.GetHTMLOfArea(list5));
                    this.ltlWebpart.Text = builder.ToString();
                }
            }
        }

        private string GetHTMLOfArea(List<SysDesktopWebPartsEntity> entitys)
        {
            if (entitys.Count > 0)
            {
                StringBuilder builder = new StringBuilder();

                foreach (SysDesktopWebPartsEntity entity in entitys)
                {
                    builder.Append("<table style=\"margin-bottom:12px;\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                    builder.Append("<tr height=\"21\" bgcolor=\"#889dc2\">");
                    builder.Append("<td width=\"11\"><img src=\"/images/index_downtitleleft.gif\"></td>");
                    builder.Append("<td class=\"title_letter\">&nbsp;" + entity.WPName + "</td>");
                    builder.Append("<td width=\"15\"><img src=\"" + (entity.Expanded == 1 ? "/images/index_arrow_up.gif" : "/images/index_arrow_down.gif") + "\" _src=\"" + entity.URL + "\" style=\"CURSOR:hand\" border=\"0\" onClick=\"hide(this)\"></td>");
                    builder.Append("<td width=\"5\"><img src=\"/images/index_downtitleright.gif\"></td>");
                    builder.Append("</tr>");
                    builder.Append("<tr>");
                    builder.Append("<td bgcolor=\"#ffffff\" align=\"center\" valign=\"middle\" colspan=\"4\" style=\"display:" + (entity.Expanded == 1 ? "" : "none") + ";border-left:1px solid #889DC2;border-right:1px solid #889DC2;border-bottom:1px solid #889DC2;height:" + (entity.Height == "" ? "200" : entity.Height) + "px\">");
                    if (entity.Expanded == 1)
                    {
                        builder.Append("<Div><iframe frameborder=\"0\" scrolling=\"no\" src=\"" + entity.URL + "\" style=\"display:none;overflow:auto; width:100%; height:100%;\" onload=\"displayIframe(this)\"></iframe><span>加载中 ...</span></Div>");
                    }
                    else
                    {
                        builder.Append("<Div/>");
                    }
                    builder.Append("</table>");
                }
                return builder.ToString();
            }
            return "";
        }
    }
}
